package com.xjrsoft.module.base.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.Enum.DeleteMarkEnum;
import com.xjrsoft.common.Enum.EnabledMarkEnum;
import com.xjrsoft.common.page.PageInput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.module.base.entity.XjrBaseDesktopscheme;
import com.xjrsoft.module.base.entity.XjrBaseDesktopschemeinfo;
import com.xjrsoft.module.base.service.IXjrBaseDesktopschemeService;
import com.xjrsoft.module.base.service.IXjrBaseDesktopschemeinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zwq
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/desktop-scheme")
@Api(value = "/desktop-scheme", tags = "桌面设计模块")
public class XjrBaseDesktopschemeController {


    @Autowired
    private IXjrBaseDesktopschemeService desktopschemeService;

    @Autowired
    private IXjrBaseDesktopschemeinfoService desktopschemeinfoService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有桌面设计")
    public Response getList(PageInput pageInput) {
        return Response.ok(desktopschemeService.getList(pageInput));
    }

    @PostMapping()
    @ApiOperation(value = "新增桌面设计")
    public Response add(@RequestBody JSONObject jsonObject) {
        return Response.status(desktopschemeService.add(jsonObject));
    }

    @PutMapping()
    @ApiOperation(value = "编辑桌面设计")
    public Response update(@RequestBody JSONObject jsonObject) {
        return Response.status(desktopschemeService.update(jsonObject));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除桌面设计")
    public Response delete(@PathVariable String id) throws Exception {
        return Response.status(desktopschemeService.delete(id));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过id获取桌面设计")
    public Response getDesktopschemeById(@PathVariable String id) throws Exception {
        return Response.ok(desktopschemeService.getDesktopschemeById(id));
    }


    @PutMapping("/copy")
    @ApiOperation(value = "复制桌面设计")
    public Response copy(@RequestParam String id) throws Exception {
        return Response.status(desktopschemeService.copy(id));
    }


    @GetMapping("/content/{moduleId}")
    @ApiOperation(value = "根据moduleId获取content")
    public Response getContentByModuleId(@PathVariable String moduleId) {
        XjrBaseDesktopscheme desktopscheme = desktopschemeService.getOne(Wrappers.<XjrBaseDesktopscheme>query().lambda().eq(XjrBaseDesktopscheme::getModuleId, moduleId).eq(XjrBaseDesktopscheme::getEnabledMark, EnabledMarkEnum.ENABLED.getCode()).eq(XjrBaseDesktopscheme::getDeleteMark, DeleteMarkEnum.NODELETE.getCode()));
        if (desktopscheme != null) {
            XjrBaseDesktopschemeinfo desktopschemeinfo = desktopschemeinfoService.getOne(Wrappers.<XjrBaseDesktopschemeinfo>query().lambda().eq(XjrBaseDesktopschemeinfo::getSchemeId, desktopscheme.getId()).eq(XjrBaseDesktopschemeinfo::getEnabledMark, EnabledMarkEnum.ENABLED.getCode()));
            if (desktopschemeinfo != null) {
                return Response.ok(desktopschemeinfo.getContent());
            }
        }
        return Response.ok();
    }

    @GetMapping("/contentById/{id}")
    @ApiOperation(value = "根据id获取content")
    public Response getContentById(@PathVariable String id) {
        XjrBaseDesktopschemeinfo desktopschemeinfo = desktopschemeinfoService.getOne(Wrappers.<XjrBaseDesktopschemeinfo>query().lambda().eq(XjrBaseDesktopschemeinfo::getSchemeId, id).eq(XjrBaseDesktopschemeinfo::getEnabledMark, EnabledMarkEnum.ENABLED.getCode()));
        if (desktopschemeinfo != null) {
            return Response.ok(desktopschemeinfo.getContent());
        }
        return Response.ok();
    }

    @PutMapping("/default")
    @ApiOperation(value = "设置为默认桌面")
    public Response setDefault(@RequestParam String id) throws Exception {
        return Response.status( desktopschemeService.setDefault(id));
    }
}
